require "import"
import "str"

config=...

this.setContentView(loadlayout({
  RelativeLayout,
  layout_width="fill",
  layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    FrameLayout,
    layout_width="fill",
    layout_height="56dp" ,
   -- gravity="left|center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      padding="9dp",
      layout_gravity=20,
      colorFilter=图标色,
    },
    {
      TextView,
      layout_gravity=17|5,
      textSize="16dp",
      textColor=文字色,
      text="选择线路",
      padding="8dp",
      onClick=function(v)
      local lines={}
      local apis=getApis()
      for k=1,table.size(apis) do
       table.insert(lines,table.size(lines)+1,"线路 "..k.."("..apis[""..k]:sub(1,1)..")")
        end
local apiType=getAccountApi()+0
lines[apiType]=lines[apiType].."（已选）"
      local pop=PopupMenu(this,v)
        local menu=pop.Menu
        local function setText(a)
          setAccountApi(tostring(table.find(lines,tostring(a))))
          url=getAccountApiUrl()
          t1url=url
        end
        for _,v in ipairs(lines) do
          menu.add(v).onMenuItemClick=setText
        end
        pop.show()
      end,
      foreground=波纹(波纹色),
    },
  },
  {
    LinearLayout,
    layout_height="-1";
    layout_width="-1";
    orientation="vertical",
    {
      LinearLayout,
      paddingTop="56dp" ,
    },
    {
    ScrollView,
    layout_height="-1";
    layout_width="-1";
    {
      LinearLayout;
      orientation="vertical";
      layout_height="-1";
      layout_width="-1";
      {
        TextView,
        layout_width="fill",
        layout_height="112dp",
        gravity="left|bottom",
        text="注册",
        textSize="32sp",
        textColor=文字色,
        padding="64dp";
        paddingTop="0";
        paddingBottom="16dp";
        layout_marginBottom="16dp";
      },
      {
        MEditText
        {
          textSize="14sp",
          id="edit1",
          hint="账号";
          textColor=文字色;
          HintTextColor=次要文字色;
          SingleLine=true;
          layout_width="-1";
          layout_height="-2";
        };
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_margin="16dp";
        layout_marginBottom="0";
      };
      {
        MEditText
        {
          textSize="14sp",
          id="edit2",
          hint="密码";
          textColor=文字色;
          HintTextColor=次要文字色;
          SingleLine=true;
          layout_width="-1";
          layout_height="-2";
        };
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_margin="16dp";
        layout_marginBottom="0";
      };
      {
        MEditText
        {
          textSize="14sp",
          id="edit3",
          hint="邀请码(选填)";
          textColor=文字色;
          HintTextColor=次要文字色;
          SingleLine=true;
          layout_width="-1";
          layout_height="-2";
        };
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_margin="16dp";
        layout_marginBottom="8dp";
      };
      {
        MEditText
        {
          textSize="14sp",
          id="edit4",
          hint="个性签名(选填)";
          textColor=文字色;
          HintTextColor=次要文字色;
          SingleLine=true;
          layout_width="-1";
          layout_height="-2";
        };
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_margin="16dp";
        layout_marginBottom="8dp";
      };
      {
        MEditText
        {
          textSize="14sp",
          id="edit5",
          hint="邮箱";
          textColor=文字色;
          HintTextColor=次要文字色;
          SingleLine=true;
          layout_width="-1";
          layout_height="-2";
        };
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_margin="16dp";
        layout_marginBottom="8dp";
      };
      {
        CardView;
        layout_width="-1";
        layout_height="-2";
        Radius="4dp";
        cardBackgroundColor=背景色;
        layout_marginTop="16dp";
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_marginBottom="8dp";
        cardElevation="0dp";
        {
          TextView;
          layout_width="-1";
          layout_height="-1";
          textSize="16sp";
          paddingRight="21dp";
          paddingLeft="21dp";
          paddingTop="12dp";
          paddingBottom="12dp";
          Text="注册";
          foreground=波纹(波纹色),
          textColor=文字色;
          id="button";
          gravity="center";
          backgroundColor=0x219e9e9e;
        };
      };
      {
        LinearLayout;
        layout_width="-1";
        layout_height="-2";
        sort("已经有账号了？"),
        paddingRight="64dp";
        paddingLeft="64dp";
        paddingTop="8dp";
        paddingBottom="0";
      };
      {
        LinearLayout;
        layout_width="-1";
        layout_height="-2";
        layout_marginTop="8dp";
        layout_marginLeft="64dp";
        layout_marginRight="64dp";
        layout_marginBottom="8dp";
        {
          TextView;
          layout_width="-1";
          layout_height="-1";
          textSize="16sp";
          paddingRight="21dp";
          paddingLeft="21dp";
          paddingTop="12dp";
          paddingBottom="12dp";
          Text="登录";
          textColor=文字色;
          foreground=波纹(波纹色),
          id="button2";
          gravity="center";
        };
       },
       {
      LinearLayout,
      gravity=17,
      layout_gravity=17,
        {
        CheckBox,
        id="button3Check",
        },
        {
          TextView,
          
          textSize="16sp";
          
          Text="用户协议与隐私政策";
          textColor=文字色;
          foreground=波纹(波纹色),
          id="button3";
          gravity="center";
        };
       },
      };
    };
  },
}))

import "android.text.InputType"
import "android.text.method.PasswordTransformationMethod"

edit2.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
edit2.setTransformationMethod( PasswordTransformationMethod());

activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

button.onClick=function()
if !button3Check.isChecked() then
toast("请先同意用户协议与隐私政策")
return
end
  
  username=edit1.Text
  password=edit2.Text
  email=edit5.Text
--  this.setDebug(true)
 -- print(email:find("%."),email:find("@"))
  if email:find("%.")==nil||email:find("@")==nil then
    toast"请输入有效邮箱"
    return
   elseif #email<7 then
    toast"请输入有效邮箱"
   return
  end
  
  toast("正在注册")
  注册账号(url,key,username,password,edit3.Text,email, edit4.Text,httk, function(返回值)
    if 返回值=="401" then
      toast("出现异常！")
     else
      data=cjson.decode(返回值)
      if data["code"]=="1" then
        toast("注册成功")
        --activity.newActivity("login")
        activity.setSharedData("username",username)
        activity.setSharedData("password",password)
        if this.getPackageName():find"and"==nil then
       -- this.setDebug(true)
        local ids=this.getSystemService("appwidget")
        .getAppWidgetIds(luajava.bindClass"android.content.ComponentName"(this,"com.windmill.widget.OneWordWidgetProvider"))
this.sendBroadcast(
Intent()
.setAction("android.appwidget.action.APPWIDGET_UPDATE")
.setClass(this--[[.getApplication()]],
luajava.bindClass"com.windmill.widget.OneWordWidgetProvider"
)
.putExtra("appWidgetIds",ids)
)
end
        activity.finish()
       elseif data["code"]=="0" then
        toast(data["message"])
      end
    end
  end)
end

button2.onClick=function()
  activity.newActivity("login")
  activity.finish()
end

button3.onClick=function(v)
local dia=showDialog(v.text,隐私政策,"我知道了")
Http.get(
--"http://xianyan.jpszx.com/?id=12", 
getApiUrl("xianyan","?id=12"),
function(c,n)
if c!=200 then
toast"无法获取"
return
end
--print(n)
dia.setMessage(
tostring(Html.fromHtml(n:match([[post%-page%-wrapper%-content">(.-)<label id="AjaxCommentBegin"></label>]])))
)
end)
dia.findViewById(16908299)--r id message
    .setTextIsSelectable(true)
end

getCloudApis()